﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once
#include <QTableWidget>
#include "ui_GraphicsStyleDlg.h"
#include "ElementId.h"
#include "IGraphicsStylePage.h"
#include "UniIdentity.h"
#include <set>
#include "OwnerPtr.h"
#include "IUiView.h"
#include "IUiDocument.h"
#include "WeakPtr.h"
#include "QDialog"

namespace gcmp
{
    class IDocument;
    class VisibilityGraphics;
    class UniIdentity;
    class IModelView;
    class IJournalUiObject;
}

class GraphicsStyleDlg : public QDialog
{
    Q_OBJECT
public:
    GraphicsStyleDlg(gcmp::IUiDocument* pUIDoc, gcmp::IUiView* currentView, std::wstring& title);
    ~GraphicsStyleDlg();
public:
    void AddTabPage(gcmp::OwnerPtr<IGraphicsStylePage> opPage);
    void UpdateTabUi();

    static bool GetImportedObjectCategories(gcmp::IDocument* pDoc, std::map<std::wstring, std::list<gcmp::UniIdentity>>& categories);

    // 获取文档中所有有样式的类别
    static std::set<gcmp::UniIdentity> GetAllCategoriesWithGStyles(gcmp::IDocument* pDoc);

private:
    void AddGraphicsStyleTable(const std::vector<gcmp::UniIdentity>& categories, const std::wstring& label);
    void ClearAllTabPages();
    void ClearAllGsTables();
    void ResetAll();

    bool IsViewGraphicsStyle() const { return m_wpCurrentView.Get() != nullptr; }
    gcmp::IModelView* GetModelView() const;

    void onMoveRequested(const QPoint& cursorPos, const QPoint& offset);
    void RefreshBtnStatus();

protected:
    virtual void changeEvent(QEvent* event) override;

private slots:
    void OnApply();
    void OnTabChanged(int iIndex);
private:
    Ui::GraphicsStyleDialog m_ui;

    gcmp::WeakPtr<gcmp::IUiView> m_wpCurrentView;
    gcmp::WeakPtr<gcmp::IUiDocument> m_wpUIDoc;

    std::vector<gcmp::OwnerPtr<IGraphicsStylePage>> m_opPages;

    gcmp::OwnerPtr<gcmp::IJournalUiObject> m_opApplyBntJournalUiObject;
    gcmp::OwnerPtr<QMouseEvent> m_opApplyBntMouseEvent;

private:
    class RecordMaterialDatas
    {
    public:
        RecordMaterialDatas(int64_t id) : m_dlgId(id) {}
        void operator()();
    private:
        int64_t m_dlgId;
    };
};
